var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cookieSession = require('cookie-session')

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(require('cors')({
  origin: true,
  credentials: true
}))

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));

// app.use('/', indexRouter);
// app.use('/users', usersRouter);

app.get('/', function (req, res) {
  res.cookie('b_index', new Date().getTime(), {
    maxAge: 1000 * 60 * 60
  });
  // console.log(`set cookie: _biz_=yh`);
  res.sendFile(__dirname + '/public/index.html');
});

app.get('/init', (req, res) => {

  console.log(JSON.stringify(req.cookies));

  res.cookie('b_init', new Date().getTime(), {
    maxAge: 1000 * 60 * 60
    // domain: 'b.xiwc.com:3000'
  })
  res.json({
    success: true
  })
})

app.get('/test', function (req, res) {
  console.log('get cookie: ' + JSON.stringify(req.cookies));

  res.json({
    success: true
  });
});

app.listen(3000, () => console.log('server started...'))

module.exports = app;